 package com.zu.commu.auto1_0.protocol.clock;

 import com.zu.commu.auto1_0.DataType1_0;import com.zu.commu.auto1_0.protocol.CheckData;import com.zu.commu.auto1_0.protocol.Util;import com.zu.commu.exception.CommuException;

 public class SetClockProtocol
 {
   public byte[] remoteData(String meterNo, String date)
     throws Exception
   {
     String temp = "";
     try {
       temp = checkValue(date);
     } catch (Exception e) {
       throw e;
     }

     byte[] buf = new byte[20];
     buf[0] = 104;
     buf[1] = 14;
     buf[2] = 14;
     buf[3] = 104;
     Util u = new Util();
     try {
       byte[] b = u.hex2Byte(meterNo);
       for (int i = 0; i < b.length; i++)
         buf[(4 + i)] = b[i];
     }
     catch (Exception e) {
       throw new CommuException().setMessage(
         "测控器地址号不合法！" + e.getMessage(), null);
     }
     buf[10] = 64;
     buf[11] = (byte)DataType1_0.SETCLOCK.toInt();
     try
     {
       byte[] b = u.hex2Byte(temp);
       for (int i = 0; i < b.length; i++)
         buf[(12 + i)] = b[i];
     }
     catch (Exception e) {
       throw new CommuException().setMessage("时钟数据不合法值！" + e.getMessage(),
         e);
     }

     buf[18] = new CheckData().getCrcCheck(buf, 4, 17);
     buf[19] = 22;
     return buf;
   }

   public String checkValue(String value)
     throws CommuException
   {
     value = new Util().cleanDotNumber(value);
     if (value.equals("")) {
       throw new CommuException().setMessage("时钟数据不合法值！", null);
     }
     if (value.startsWith("-")) {
       throw new CommuException().setMessage("时钟数据不合法值！", null);
     }
     if (value.length() > 12) {
       value = value.substring(0, 12);
     }
     long temp = 0L;
     try {
       temp = Long.parseLong(value);
     } catch (Exception e) {
       throw new CommuException().setMessage("时钟数据不合法值！", null);
     }
     String s = temp+"";
     while (s.length() < 12) {
       s = "0" + s;
     }
     return s;
   }
 }

